<template>
  <drag-dialog v-model="showDialog" title="数据配置">
    <el-tabs>
      <el-tab-pane label="静态json数据">
        <el-input type="textarea" :rows="10" v-model="localData" />
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <dialog-footer @ok="submit" @cancel="cancel" />
    </template>
  </drag-dialog>
</template>

<script>
export default {
  props: {
    modelValue: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      showDialog: false,
      localData: ''
    };
  },
  watch: {
    modelValue: {
      handler(val) {
        this.showDialog = val;
      },
      immediate: true
    },
    showDialog(val) {
      this.$emit('update:modelValue', val);
    },
    data: {
      handler(val) {
        if (typeof val == 'string') {
          this.localData = val;
        } else {
          this.localData = JSON.stringify(val);
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    submit() {
      try {
        const options = JSON.parse(this.localData);
        this.$emit('update', options);
        this.cancel();
      } catch (e) {
        console.error('错误的json', e);
        this.msgError('非法的json格式', e);
      }
    },
    cancel() {
      this.showDialog = false;
    }
  }
};
</script>

<style></style>
